﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace HCI.Questionaire.Controls
{
    public class Animate
    {
        public static Storyboard CreateTranslateAndScaleIn(DependencyObject objectToBeAnimated, double fromX, double fromY, double toX, double toY)
        {
            Storyboard sb = new Storyboard();

            DoubleAnimationUsingKeyFrames animScaleX = new DoubleAnimationUsingKeyFrames();
            DoubleAnimationUsingKeyFrames animScaleY = new DoubleAnimationUsingKeyFrames();
            DoubleAnimationUsingKeyFrames animTranslateX = new DoubleAnimationUsingKeyFrames();
            DoubleAnimationUsingKeyFrames animTranslateY = new DoubleAnimationUsingKeyFrames();
            DoubleAnimationUsingKeyFrames animOpacity = new DoubleAnimationUsingKeyFrames();


            // scaleX animation

            animScaleX.BeginTime = new TimeSpan(0);
            animScaleX.KeyFrames.Add(getSplineDoubleKeyFrame(0, 0));
            animScaleX.KeyFrames.Add(getSplineDoubleKeyFrame(0.2, 1));
            sb.Children.Add(animScaleX);

            Storyboard.SetTarget(animScaleX, objectToBeAnimated);
            Storyboard.SetTargetProperty(animScaleX, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)"));


            // scaleY animation

            animScaleY.BeginTime = new TimeSpan(0);
            animScaleY.KeyFrames.Add(getSplineDoubleKeyFrame(0, 0));
            animScaleY.KeyFrames.Add(getSplineDoubleKeyFrame(0.2, 1));
            sb.Children.Add(animScaleY);

            Storyboard.SetTarget(animScaleY, objectToBeAnimated);
            Storyboard.SetTargetProperty(animScaleY, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)"));


            animTranslateX.BeginTime = new TimeSpan(0);
            animTranslateX.KeyFrames.Add(getSplineDoubleKeyFrame(0, fromX));
            animTranslateX.KeyFrames.Add(getSplineDoubleKeyFrame(0.2, toX));
            sb.Children.Add(animTranslateX);

            Storyboard.SetTarget(animTranslateX, objectToBeAnimated);
            Storyboard.SetTargetProperty(animTranslateX, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)"));


            animTranslateY.BeginTime = new TimeSpan(0);
            animTranslateY.KeyFrames.Add(getSplineDoubleKeyFrame(0, fromY));
            animTranslateY.KeyFrames.Add(getSplineDoubleKeyFrame(0.2, toY));
            sb.Children.Add(animTranslateY);

            Storyboard.SetTarget(animTranslateY, objectToBeAnimated);
            Storyboard.SetTargetProperty(animTranslateY, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)"));

            animOpacity.BeginTime = new TimeSpan(0);
            animOpacity.KeyFrames.Add(getSplineDoubleKeyFrame(0, 0));
            animOpacity.KeyFrames.Add(getSplineDoubleKeyFrame(0.1, 1));
            sb.Children.Add(animOpacity);

            Storyboard.SetTarget(animOpacity, objectToBeAnimated);
            Storyboard.SetTargetProperty(animOpacity, new PropertyPath("(UIElement.Opacity)"));


            return sb;
        }


        public static Storyboard CreateTranslateAndScaleOut(DependencyObject objectToBeAnimated, double fromX, double fromY, double toX, double toY)
        {
            Storyboard sb = new Storyboard();

            DoubleAnimationUsingKeyFrames animScaleX = new DoubleAnimationUsingKeyFrames();
            DoubleAnimationUsingKeyFrames animScaleY = new DoubleAnimationUsingKeyFrames();
            DoubleAnimationUsingKeyFrames animTranslateX = new DoubleAnimationUsingKeyFrames();
            DoubleAnimationUsingKeyFrames animTranslateY = new DoubleAnimationUsingKeyFrames();
            DoubleAnimationUsingKeyFrames animOpacity = new DoubleAnimationUsingKeyFrames();

            // scaleX animation

            animScaleX.BeginTime = new TimeSpan(0);
            animScaleX.KeyFrames.Add(getSplineDoubleKeyFrame(0, 1));
            animScaleX.KeyFrames.Add(getSplineDoubleKeyFrame(0.2, 0));
            sb.Children.Add(animScaleX);

            Storyboard.SetTarget(animScaleX, objectToBeAnimated);
            Storyboard.SetTargetProperty(animScaleX, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)"));


            // scaleY animation

            animScaleY.BeginTime = new TimeSpan(0);
            animScaleY.KeyFrames.Add(getSplineDoubleKeyFrame(0, 1));
            animScaleY.KeyFrames.Add(getSplineDoubleKeyFrame(0.2, 0));
            sb.Children.Add(animScaleY);

            Storyboard.SetTarget(animScaleY, objectToBeAnimated);
            Storyboard.SetTargetProperty(animScaleY, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)"));


            animTranslateX.BeginTime = new TimeSpan(0);
            animTranslateX.KeyFrames.Add(getSplineDoubleKeyFrame(0, toX));
            animTranslateX.KeyFrames.Add(getSplineDoubleKeyFrame(0.2, fromX));
            sb.Children.Add(animTranslateX);

            Storyboard.SetTarget(animTranslateX, objectToBeAnimated);
            Storyboard.SetTargetProperty(animTranslateX, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)"));


            animTranslateY.BeginTime = new TimeSpan(0);
            animTranslateY.KeyFrames.Add(getSplineDoubleKeyFrame(0, toY));
            animTranslateY.KeyFrames.Add(getSplineDoubleKeyFrame(0.2, fromY));
            sb.Children.Add(animTranslateY);

            Storyboard.SetTarget(animTranslateY, objectToBeAnimated);
            Storyboard.SetTargetProperty(animTranslateY, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)"));

            animOpacity.BeginTime = new TimeSpan(0);
            animOpacity.KeyFrames.Add(getSplineDoubleKeyFrame(0.1, 1));
            animOpacity.KeyFrames.Add(getSplineDoubleKeyFrame(0.2, 0));
            sb.Children.Add(animOpacity);

            Storyboard.SetTarget(animOpacity, objectToBeAnimated);
            Storyboard.SetTargetProperty(animOpacity, new PropertyPath("(UIElement.Opacity)"));


            return sb;
        }



        /// <summary>
        /// Helper method - create SplineDoubleKeyFrame, based on seconds for duration & value to animate to
        /// </summary>
        /// <param name="seconds"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        private static SplineDoubleKeyFrame getSplineDoubleKeyFrame(double seconds, double value)
        {
            return new SplineDoubleKeyFrame()
            {
                KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(seconds))
               ,
                Value = value
            };
        }


    }
}
